#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: customlabel.py
# Author: Tomás Vírseda
# License: GPL v3
# Description:  Action Bar launcher.

"""
"""
import os

import gtk

from vazaarlib.constants import GUI_DIR
from vazaarlib.ui.glade import Widgets
from vazaarlib.utils import get_logger


class ActionBarManager:
    """ status bar manager """

    widget = {}
    working = True

    def __init__(self, gui):
        self.gui = gui
        self.log = get_logger("ActionBar")

        self.gui.frontend['actionbar'] = self.gui.glade.widgets.get_widget("actionbar")
        self.gui.frontend['starthere'] = self.gui.glade.widgets.get_widget("btnStartHere")
        self.gui.frontend['browser'] = self.gui.glade.widgets.get_widget("btnBrowser")
        self.gui.frontend['explorer'] = self.gui.glade.widgets.get_widget("btnExplorer")
        self.gui.frontend['query'] = self.gui.glade.widgets.get_widget("btnQuery")
        self.gui.frontend['import'] = self.gui.glade.widgets.get_widget("btnAddResource")
        #self.gui.frontend['delres'] = self.gui.glade.widgets.get_widget("btnDelRes")
        #self.gui.frontend['search'] = self.gui.glade.widgets.get_widget("etySearch")
        #self.gui.frontend['chkview'] = self.gui.glade.widgets.get_widget("chkThisView")
        #self.gui.frontend['chkword'] = self.gui.glade.widgets.get_widget("chkMatchWord")
        #self.gui.frontend['chkcase'] = self.gui.glade.widgets.get_widget("chkMatchCase")
        self.gui.frontend['desktoplog'] = self.gui.glade.widgets.get_widget("lblDesktopLog")
        self.gui.frontend['fullscreen'] = self.gui.glade.widgets.get_widget("tgbFullscreen")

        self.gui.frontend['connections'] = {
            'btnStartHere/clicked' :    self.starthere,
            'btnBrowser/clicked' :      self.browser,
            'btnExplorer/clicked' :     self.explorer,
            'btnQuery/clicked':         self.queries,
            'btnAddResource/clicked' :   self.create_resource,
            #'btnDelRes/clicked' :   self.delete_resource,
            'tgbFullscreen/toggled' :   self.toggle_fullscreen,
            #'etySearch/activate' :      self.search_term
        }
        #connect signals
        for wid_con, func in self.gui.frontend['connections'].iteritems():
            wid,con = wid_con.split('/')
            self.gui.glade[wid].connect(con,func)

    def delete_resource(self, *args):
        """This code deletes all selected resources in StartHere.
           1. get all selected resources
           2. foreach resource:
              2.1. Delete it physically
              2.2. Delete it from KB
              2.3. Disasocciate from any collection
              2.4. Delete it from cache
              2.5. Delete entry from customview

           FIXME: implement the Trash resource and add deleted resources to it.
        """
        customviews = []
        #customviews.append(self.gui.frontend['sh-clipboard'])
        #customviews.append(self.gui.frontend['sh-collections'])
        customviews.append(self.gui.frontend['sh-resources'])

        selected_resources = {}
        for customview in customviews:

            liststore = customview.get_children()[0]
            try:
                # Get selected resources from listviews
                model, selected = liststore.listview.selection.get_selected_rows()
                selected_resources[model] = selected
            except:
                # Get selected items from iconviews
                selected = liststore.view.get_selected_items()
                model = liststore.view.get_model()
                selected_resources[model] = selected


        for model in selected_resources:
            paths = selected_resources[model]
            for path in paths:
                try:
                    iter = model.get_iter_from_string(str(path[0]))
                    id = model.get_value(iter, 0)
                    """
                    collection = self.gui.ask.resource_is_collection(id)
                    for resource in collection:
                        self.gui.actions.delete_resource(resource)
                    """
                    #print "deleting resource", id
                    self.gui.actions.delete_resource(id)
                except Exception, error:
                    print error
        self.gui.actions.update_gui()

    def view_is_checked(self):
        return self.gui.frontend['chkview'].get_active()

    def starthere(self, widget=None):
        self.gui.manager.show_start_here()

    def browser(self, widget=None):
        self.gui.manager.show_browser()

    def explorer(self, widget=None):
        self.gui.manager.show_explorer()

    def queries(self, widget=None):
        self.gui.manager.show_queries()

    def create_resource(self, widget=None):
        #FIXME: something wrong here
        try:
            #self.gui.actions.hide_application()
            self.gui.addresdlg.show_window()
        except Exception, error:
            print error

    def toggle_fullscreen(self, widget=None):
        if (self.gui.frontend['fullscreen'].get_active()):
            self.gui.actions.fullscreen()
        else:
            self.gui.actions.unfullscreen()


    def search_term(self, widget=None):
        self.log.debug("Search function not implemented")
        #self.gui.search_term(widget)
